package com.zb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zb.common.BaseContext;
import com.zb.common.R;
import com.zb.entity.Employee;
import com.zb.entity.LoginData;
import com.zb.entity.PageData;
import com.zb.service.EmployeeService;
import com.zb.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    static Employee loginInfo = null;
    @Autowired
    private EmployeeService employeeService;

    //登录
    @PostMapping("/login")
    public R<LoginData> login(@RequestBody Employee employee) {
        //密码MD5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        if (emp == null) {
            return R.error("员工不存在");
        }
        if (!emp.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        if (emp.getStatus() == 0) {
            return R.error("账户已禁用");
        }
        loginInfo = emp;
        long id = emp.getId();

        BaseContext.setThreadLocal(id);

        Map<String, Object> claims = new HashMap<>();
        claims.put("username", emp.getUsername());
        claims.put("password", emp.getPassword());


        String token = JwtUtils.getToken(claims);

        LoginData data = new LoginData();
        data.setId(emp.getId());
        data.setName(emp.getName());
        data.setUsername(emp.getUsername());
        data.setPassword(emp.getPassword());
        data.setToken(token);
//        emp.setToken(token);
        return R.success(data, "登录成功");
    }


    //根据id、查询用户信息
    @GetMapping("/{id}")
    public R<Employee> getEmpInfoById(@PathVariable Long id) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getId, id);
        Employee emp = employeeService.getOne(queryWrapper);
        return R.success(emp, "获取用户信息成功");
    }

    //新增员工
    @PostMapping("/add")
    public R<Boolean> addEmp(@RequestBody Employee employee) {
        //设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        Boolean boo = employeeService.save(employee);
        return R.success(boo, "新增成功");
    }


    //分页查询用户
    @GetMapping("select")
    public R<PageData> selectEmp(Integer page, Integer pageSize, String name) {

        PageData pageData = employeeService.pageSelect(page, pageSize, name);
        return R.success(pageData, "查询员工成功");
    }


    //查询所有员工信息
    @GetMapping("/select/all")
    public R<List<Employee>> selectAllEmp() {
        List<Employee> list = employeeService.selectAllEmp();
        return R.success(list, "查询成功");
    }


    //更新员工信息
    @PutMapping("/update")
    public R<Boolean> updateEmpInfo(@RequestBody Employee employee) {
        Integer num = employeeService.updateEmpInfo(employee);
        if (num > 0) {
            return R.success(true, "更新成功");
        }
        return R.success(false, "用户不存在");
    }

//    @DeleteMapping("/{empId}")
//    public R<Integer> deleteEmp(@PathVariable String empId) {
//        int num = employeeService.deleteEmpById(empId);
//        return R.success(num, "删除成功");
//    }


}
